package com.ruoyi.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.domain.Comment;
import com.ruoyi.business.domain.CourseComment;
import com.ruoyi.business.domain.bo.CommentBo;
import com.ruoyi.business.domain.vo.CommentVo;
import com.ruoyi.business.mapper.CommentMapper;
import com.ruoyi.business.mapper.CourseCommentMapper;
import com.ruoyi.business.service.ICommentService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 最爱喝兽奶
 * @create 2022/7/30
 **/
@SuppressWarnings("ALL")
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private CourseCommentMapper courseCommentMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public List<CommentVo> queryCommentByCourseId(String courseId) {
        return null;
    }

    @Override
    public void insertComment(CommentBo bo) {
        Comment comment = new Comment();
        BeanUtils.copyBeanProp(comment, bo, false);
        commentMapper.insert(comment);
        bo.setId(comment.getId());
    }

    @Override
    public TableDataInfo<CommentVo> queryCourseList(String courseId, String pageSize, String pageNum) {
        Page<Comment> page = baseMapper.selectPage(PageUtils.buildPage(null), buildCourseCommentWrapper(courseId));
        List<CommentVo> vos = page.getRecords().stream().map(item -> {
            CommentVo vo = new CommentVo();
            BeanUtils.copyBeanProp(vo, item, true);
            List<Comment> children = new ArrayList<>();
            // 设置vo的子评论
            setChildrenComment(vo.getId(), children);
            vo.setChildren(children);
            // 设置用户
            vo.setCommentUserVo(sysUserMapper.selectUserById(vo.getCommentUser()));
            return vo;
        }).collect(Collectors.toList());
        Page<CommentVo> result = PageUtils.convert(page, vos);
        return PageUtils.buildTableDataInfo(result);
    }



    @Override
    public Boolean insertCommentForCourse(CommentBo bo, String courseId) {
        Comment comment = new Comment();
        BeanUtils.copyBeanProp(comment, bo, false);

        Boolean result = false;
        if(commentMapper.insert(comment)==1)
        {
            String commentId = comment.getId();
            CourseComment courseComment = new CourseComment();
            courseComment.setCommentId(comment.getId());
            courseComment.setCourseId(courseId);
            result = (1==courseCommentMapper.insert(courseComment));
        }

        return result;
    }


    /**
     *
     * @param parentId
     * @param children
     */
    private void setChildrenComment(String parentId, List<Comment> children) {
        if (children.size() >= 10) return;
            // 第一次进入的时候
        List<Comment> comments = commentMapper.selectList(new QueryWrapper<Comment>().eq("parent_id", parentId));
        for (Comment comment : comments) {
            if (children.size() >= 10) return;
            if (null != comment.getCommentUser()) {
                SysUser sysUser = sysUserMapper.selectUserById(comment.getCommentUser());
                comment.setCommentUserVo(sysUser);
            }
            children.add(comment);
            setChildrenComment(comment.getId(), children);
        }
    }

    private LambdaQueryWrapper buildCourseCommentWrapper(String courseId) {
        LambdaQueryWrapper<Comment> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNotBlank(courseId), Comment::getCourseId, courseId);
        lqw.orderBy(true, true, Comment::getCreateTime);
        return lqw;
    }
}
